//
//  HWBookReadViewController.m
//  嗨文
//
//  Created by 响当当 on 5/14/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HWBookReadViewController.h"
#import "HWBookReadView.h"

@interface HWBookReadViewController ()
@property (nonatomic, strong)HWBookReadView *bookReadView;
@property (nonatomic, assign)BOOL showOrHideBarStatue;
@end

@implementation HWBookReadViewController

- (HWBookReadView*)bookReadView
{
    if (!_bookReadView)
    {
        _bookReadView = [[HWBookReadView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT)];
        WeakSelf(self)
        _bookReadView.changeStatueBarColorBlock = ^(UIColor *color) {
//            [weakSelf setStatusBarBackgroundColor:color];
        };
        
        _bookReadView.nextPageContextBlock = ^(NSString *nextChapterId, NSString *nextChapterCoin) {
            [weakSelf payChapter:nextChapterCoin chapterId:nextChapterId];
        };
        
        _bookReadView.upPageContextBlock = ^(NSString *upChapterId, NSString *upChapterCoin) {
            [weakSelf payChapter:upChapterCoin chapterId:upChapterId];
        };
        _bookReadView.loadChapterBlock = ^(NSString *chapterId, NSString *chapterCoin) {
            [weakSelf getInfor:chapterId chapterCoin:chapterCoin];
            weakSelf.chapterId = chapterId;
            weakSelf.chapterCoin = chapterCoin;
        };
        _bookReadView.getBackBlock = ^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
        _bookReadView.showOrHideStatueBarBlock = ^(BOOL show) {
            
        };
    }
    return _bookReadView;
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.showOrHideBarStatue = YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    [self setDefaultNav];
    
    [self.view addSubview:self.bookReadView];
//    [self setStatusBarBackgroundColor:LCHexColor(0xf9f9f9)];
    
    [self prefersStatusBarHidden];
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self buryPoint];
    
    [self getInfor:self.chapterId chapterCoin:self.chapterCoin];
    
}

#pragma mark - 获取小说内容
- (void)getInfor:(NSString *)chapterId chapterCoin:(NSString *)chapterCoin
{
    
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"chapterCoin" : chapterCoin,
                               @"chapterId" : chapterId
                               };
    
    WeakSelf(self)
    [MyInfoManager getBookContext:prarmers sucess:^(id  _Nonnull acceptData)
    {
        if (!IFNULL(acceptData,nil))
        {
            [weakSelf.bookReadView showPayView];
        }
        else
        {
            HWBookReaderModel *model = [[HWBookReaderModel alloc]initWithDic:acceptData];
            [weakSelf.bookReadView getDate:model freeBookNum:weakSelf.chapterFreeNum];
        };
    } fail:^(NSError * _Nullable error) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showErrorWithStatus:@"获取失败，请检查网络是否良好"];
        DISMISS_SVP(1.0f);
    }];
}

- (void)payChapter:(NSString *)chapterCoin chapterId:(NSString *)chapterId
{
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"chapterCoin" : chapterCoin,
                               @"chapterId" : chapterId
                               };
    
    WeakSelf(self)
    [MyInfoManager getBookContextPay:prarmers sucess:^(id  _Nonnull acceptData)
     {
         [SVProgressHUD dismiss];
         if (!IFNULL(acceptData,nil))
         {
             [weakSelf.bookReadView showPayView];
         }
         else
         {
             HWBookReaderModel *model = [[HWBookReaderModel alloc]initWithDic:acceptData];
             [weakSelf.bookReadView getDate:model freeBookNum:weakSelf.chapterFreeNum];
         }
         
     } fail:^(NSError * _Nullable error) {
         DISMISS_SVP(1.0f);
         [SVProgressHUD showErrorWithStatus:@"获取失败，请检查网络是否良好"];
         DISMISS_SVP(1.0f);
     }];
}


//- (void)setStatusBarBackgroundColor:(UIColor *)color
//{
//    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
//    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)])
//    {
//        statusBar.backgroundColor = color;
//    }
//}
//
//- (BOOL)prefersStatusBarHidden
//{
//    if (self.showOrHideBarStatue)
//    {
//        return YES;
//    }
//    else
//    {
//        return NO;
//    }
//}

- (void)buryPoint
{
    NSString *lastStayTime = [kUserDefaults objectForKey:@"time"];
    NSString *nowStayTime = [NSString stringWithFormat:@"%ld",[[HWUtilties getNowTimeTimestamp3] integerValue] - lastStayTime.integerValue];
    
    [HWUtilties buryPointWithStayTime:nowStayTime
                            timestamp:[HWUtilties getNowTimeTimestamp3]
                             memberId:current_User.userID
                              fromUrl:nil
                                toUrl:@"/reading"
                               fromId:nil
                                 toId:@"40cea061fd5986f2"];
}

@end
